/*
 * EnrollFragment.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.enroll;

import android.content.DialogInterface;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseEventFragment;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.data.event.RefreshEnrollResultStateEvent;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.ui.widget.dialog.BusinessDialog;
import com.yidiandao.business.ui.widget.dialog.ChoosePayWayDialog;
import com.yidiandao.business.ui.widget.view.GoodsCountMenu;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 填写报名信息 - View
 */
public class EnrollFragment extends BaseEventFragment implements EnrollContract.View, ChoosePayWayDialog.OnConfirmPayWayListener, GoodsCountMenu.OnGoodsCountChangeListener {

    private EnrollContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private TextView mTvActiveName;

    private EditText mTvUserName;

    private EditText mTvPhone;

    private GoodsCountMenu mChooseCountMenu;

    private Button mBtnSubmit;

    private TextView mTvMoney;

    private TextView mTvPayWay;

    private RelativeLayout mRootChoosePayWay;

    public static EnrollFragment getInstance() {
        EnrollFragment fragment = new EnrollFragment();
        return fragment;
    }

    @Override
    public void setPresenter(EnrollContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return EnrollFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvActiveName = (TextView) mRootView.findViewById(R.id.tv_active_name);
        mTvUserName = (EditText) mRootView.findViewById(R.id.tv_user_name);
        mTvPhone = (EditText) mRootView.findViewById(R.id.tv_phone);
        mChooseCountMenu = (GoodsCountMenu) mRootView.findViewById(R.id.choose_count_menu);
        mBtnSubmit = (Button) mRootView.findViewById(R.id.btn_submit);
        mTvMoney = (TextView) mRootView.findViewById(R.id.tv_money);
        mTvPayWay = (TextView) mRootView.findViewById(R.id.tv_pay_way);
        mRootChoosePayWay = (RelativeLayout) mRootView.findViewById(R.id.root_choose_pay_way);

        // 人数最少为1
        mChooseCountMenu.setMinGoodsCount(1);
        mChooseCountMenu.setMenuMode(GoodsCountMenu.MODE_EDIT);

        mPresenter.start();
        initListeners();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.enroll();
            }
        });

        mRootChoosePayWay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChoosePayWayDialog();
            }
        });

        mChooseCountMenu.setOnGoodsCountChangeListener(this);
    }

    public void showChoosePayWayDialog() {
        ChoosePayWayDialog dialog = new ChoosePayWayDialog(mActivity);
        dialog.show();
        dialog.setPayWays(mPresenter.getPayWays());
        dialog.setOnConfirmPayWayListener(this);
    }

    @Override
    public String getUserName() {
        return mTvUserName.getText().toString().trim();
    }

    @Override
    public String getPhoneNumber() {
        return mTvPhone.getText().toString().trim();
    }

    @Override
    public int getPersonNumber() {
        return mChooseCountMenu.getGoodsCount();
    }

    @Override
    public void showFailedDialog(String message) {
        new BusinessDialog.Builder(mActivity)
                .setTitle(R.string.submit_failed)
                .setMessage(message)
                .setSinglePositiveButton(R.string.rewrite, null)
                .create()
                .show();
    }

    @Override
    public void showSuccessDialog() {
        new BusinessDialog.Builder(mActivity)
                .setTitle(R.string.submit_success)
                .setCancelable(false)
                .setMessage(R.string.thanks_for_apply)
                .setSinglePositiveButton(R.string.back_to_detail, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mActivity.finish();
                    }
                })
                .create()
                .show();
    }

    @Override
    public void setContent(String enrollTitle) {
        mTvActiveName.setText(enrollTitle);
    }

    @Override
    public void setMaxPerson(int maxPerson) {
        mChooseCountMenu.setMaxGoodsCount(maxPerson);
        mChooseCountMenu.setDialogOverStockTip(UIUtils.getString(R.string.max_enroll_person, maxPerson));
    }

    @Override
    public void refreshEnrollMoney(float enrollMoney) {
        mTvMoney.setText(TextRenderUtils.getBeautyPrice(enrollMoney + "", null, UIUtils.dp2px(15), UIUtils.dp2px(12)));
    }

    @Override
    public void setChoosePayWayLayoutVisibility(boolean flag) {
        mRootChoosePayWay.setVisibility(flag ? View.VISIBLE : View.GONE);
    }

    @Override
    public void refreshPayWay(PayWay payWay) {
        mTvPayWay.setText(payWay.getName());
    }

    @Override
    public void toOnLinePayActivity(String url) {
        UrlActivity.actionStartForEnrollOnlinePay(mActivity, url, mPresenter.getRecId());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_enroll;
    }

    @Override
    public void onConfirmPayWay(PayWay payWay) {
        mTvPayWay.setText(payWay.getName());
        mPresenter.onPayWayChanged(payWay);
    }

    @Override
    public void onGoodsCountChanged(int count) {
        mPresenter.onPersonCountChanged(count);
    }

    @Override
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RefreshEnrollResultStateEvent event) {
        // 用户报名成功
        if (event.isOnlinePay() && event.isPaySuccess()) {
            showSuccessDialog();
        }
    }

    @Override
    public void notifyDataChanged() {

    }
}
